package Example11;


import org.apache.commons.math3.random.RandomGenerator;
import org.apache.commons.math3.random.RandomGeneratorFactory;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.time.LocalDateTime;
import java.util.Base64;
import java.util.Enumeration;
import java.util.Random;

public class HelloServlet extends HttpServlet{

    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        Enumeration headerNames = req.getHeaderNames();
        System.out.println("遍历请求报文的首部:");
        while (headerNames.hasMoreElements()){
            String headerName = (String) headerNames.nextElement();
            String header = req.getHeader(headerName);
            System.out.println(headerName+" : "+header);

        }


        Cookie userIdCookie=new Cookie("uid",new String(Base64.getEncoder().encode("cimutongzi".getBytes())));
        resp.addCookie(userIdCookie);
        req.setAttribute("title","测试请求报文首部");
        req.setAttribute("author","小颜");
        req.setAttribute("content","正在测试请求报文首部,注意IDEA下的java项目是如何转成javaWeb项目的!!!");
        req.setAttribute("dateTime", LocalDateTime.now());
        req.getRequestDispatcher("index.jsp").forward(req,resp);
    }

    @Override
    protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {

    }
}
